package com.skytech.tax.customsdatawarehouse.singlewindow.processtrade.manual.domain.dao;

import com.skytech.tax.customsdatawarehouse.singlewindow.processtrade.manual.domain.entity.ManualInvtSimple;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Lianhong
 * @since 2021/11/11
 */
@Repository
public class ManualInvtSimpleDao {

    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private String saveInvtSimpleSql = "INSERT INTO sw_manual_invt_simple (seqNo) VALUES (:seqNo)";

    public void save(ManualInvtSimple simple) {
        namedParameterJdbcTemplate.update(this.saveInvtSimpleSql, new BeanPropertySqlParameterSource(simple));
    }

    private String deleteInvtSimpleSql = "DELETE FROM sw_manual_invt_simple WHERE seqNo = ?";

    public void delete(String seqNo) {
        this.namedParameterJdbcTemplate.getJdbcOperations().update(this.deleteInvtSimpleSql, seqNo);
    }

    private String existsInvtSql = "SELECT count(1) FROM sw_manual_invt_simple WHERE seqNo = ?";

    public boolean exists(String seqNo) {
        Integer count = namedParameterJdbcTemplate.getJdbcOperations()
                .queryForObject(this.existsInvtSql, new Object[]{seqNo}, Integer.class);
        return count != null && count > 0;
    }

    private String existsInvtsSql = "SELECT seqNo FROM sw_manual_invt_simple WHERE seqNo in (:seqNos)";

    public List<String> exists(List<String> seqNos) {
        Map<String, Object> params = new HashMap<>();
        params.put("seqNos", seqNos);
        return namedParameterJdbcTemplate.queryForList(this.existsInvtsSql, params, String.class);
    }
}
